package com.guan.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guan.commonutils.JwtUtils;
import com.guan.commonutils.R;
import com.guan.commonutils.ordervo.CourseWebVoOrder;
import com.guan.eduservice.client.OrdersClient;
import com.guan.eduservice.entity.EduCourse;
import com.guan.eduservice.entity.chapter.ChapterVo;
import com.guan.eduservice.entity.frontVo.CourseFrontVo;
import com.guan.eduservice.entity.frontVo.CourseWebVo;
import com.guan.eduservice.service.EduChapterService;
import com.guan.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2023/4/23 9:15
 * @Author: 管奇安
 * @Description:
 */
@RestController
@RequestMapping("/eduservice/coursefront/")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrdersClient ordersClient;

    /**
     * @date: 2023/4/23 10:46
     * @author: 管奇安
     * @description: 查询课程 条件+分页
     * @param: [page, limit, courseFrontVo]
     * @return: com.guan.commonutils.R
     **/
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false)CourseFrontVo courseFrontVo){

        Page<EduCourse> coursePage = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(coursePage,courseFrontVo);

        return R.ok().data(map);


    }

    /**
     * @date: 2023/4/25 15:25
     * @author: 管奇安
     * @description: 查询课程详情
     * @param: [couseId]
     * @return: com.guan.commonutils.R
     **/
    @GetMapping("getFrontCourseInfo/{couseId}")
    public R getFrontCourseInfo(@PathVariable String couseId, HttpServletRequest request){
        //根据课程id 编写sql查询相关信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(couseId);

        //根据课程id  查询章节小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoBycourseId(couseId);

        //根据课程id用户id查询当前课程是否已支付
        boolean buyCourse = ordersClient.isBuyCourse(couseId, JwtUtils.getMemberIdByJwtToken(request));

        return R.ok()
                .data("courseWebVo",courseWebVo)
                .data("chapterVideoList",chapterVideoList)
                .data("isBuy",buyCourse);
    }


    /**
     * @date: 2023/4/24 17:10
     * @author: 管奇安
     * @description: 根据课程id查询课程信息 远程调用
     * @param: [id]
     * @return: com.guan.commonutils.ordervo.CourseWebVoOrder
     **/
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseWebVo  = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo,courseWebVoOrder);
        return courseWebVoOrder;
    }


}
